package com.example.xrecyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jianghejie on 15/11/26.
 */
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseAdapter.BaseViewHolder> {
    public List<T> datas = new ArrayList<>();
    public ViewUtils viewUtils;
    public Context context;


    public BaseAdapter(Context context) {
        this.context = context;

    }

    //创建新View，被LayoutManager所调用
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(bindLayoutId(),viewGroup,false);
        viewUtils = new ViewUtils(view);
        return bindViewHolder(viewUtils);
    }

    /**
     * 设置布局
     *
     * @return
     */
    public abstract BaseViewHolder bindViewHolder(ViewUtils viewUtils);
    /**
     * 设置布局
     *
     * @return
     */
    public abstract int bindLayoutId();
    /**
     * 初始化布局
     *
     * @return
     */
    public abstract void initViewsAndValues(BaseViewHolder viewHolder, int position);

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(BaseViewHolder viewHolder, int position) {


        initViewsAndValues(viewHolder,position);
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public  static class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View view) {
            super(view);

        }
    }
    //自动刷新XRecyclerView
    public void updataData(XRecyclerView recyclerView, List<T> mdatas) {

        if (mdatas == null) {//是否新数据为空
            return;
        } else {
            if (datas == null) {//总数据源是否为空
                datas = mdatas;
            } else {
                if (recyclerView.getCurrentPage() == 0) {//首页
                    datas.clear();

                    recyclerView.refreshComplete();
                }else{//第二页起

                    recyclerView.loadMoreComplete();
                }
                if (mdatas.size()>0){//有数据
                    datas.addAll(mdatas);
                    notifyDataSetChanged();
                }

            }

        }

        if (mdatas.size() < recyclerView.getItemNumOfPage()) {//是否够一页数据
            recyclerView.setNoMore(true);
        } else {
            recyclerView.setNoMore(false);
            if (recyclerView.getCurrentPage() == 0) {//首页
                recyclerView.refreshComplete();
            }else{
                recyclerView.loadMoreComplete();
            }

        }


    }

    /**
     * 更新一条数据
     *
     * @param position
     * @param data
     */
    public void updateOneItem(int position, T data) {
        if (this.datas == null) {
            this.datas = new ArrayList<T>();
        }
        this.datas.set(position, data);
        notifyDataSetChanged();
    }
}
